﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace SMS
{
    public partial class Form1 : Form
    {
        #region 调用sms.dll
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        /// 
        [STAThread]

        [DllImport("sms.dll", EntryPoint = "Sms_Connection")]
        public static extern uint Sms_Connection(string CopyRight, uint Com_Port, uint Com_BaudRate, out string Mobile_Type, out string CopyRightToCOM);

        [DllImport("sms.dll", EntryPoint = "Sms_Disconnection")]
        public static extern uint Sms_Disconnection();

        [DllImport("sms.dll", EntryPoint = "Sms_Send")]
        public static extern uint Sms_Send(string Sms_TelNum, string Sms_Text);

        [DllImport("sms.dll", EntryPoint = "Sms_Receive")]
        public static extern uint Sms_Receive(string Sms_Type, out string Sms_Text);

        [DllImport("sms.dll", EntryPoint = "Sms_Delete")]
        public static extern uint Sms_Delete(string Sms_Index);

        [DllImport("sms.dll", EntryPoint = "Sms_AutoFlag")]
        public static extern uint Sms_AutoFlag();

        [DllImport("sms.dll", EntryPoint = "Sms_NewFlag")]
        #endregion

        public static extern uint Sms_NewFlag();

        HttpHelper http = new HttpHelper();
        List<BBPA.Model.SMS> listSMS_waitSend = new List<BBPA.Model.SMS>();
        public Form1()
        {
            InitializeComponent();
            http.Encoding = System.Text.Encoding.UTF8;
            Connect();
        }
        void Connect()
        {
            String TypeStr = "";
            String CopyRightToCOM = "";
            String CopyRightStr = "//上海迅赛信息技术有限公司,网址www.xunsai.com//";

            uint com =Convert.ToUInt32( System.Configuration.ConfigurationManager.AppSettings["com"]);
            if (Sms_Connection(CopyRightStr, com, 9600, out TypeStr, out CopyRightToCOM) == 1) ///5为串口号，0为红外接口，1,2,3,...为串口
            {
                //MessageBox.Show("OK");
                lb_State_SMS.Text = "短信猫已连接";
                timer_Send.Enabled = true;
                //timer1.Enabled = true;
            }
            else
            {
                lb_State_SMS.Text = "短信猫连接失败";
                MessageBox.Show("短信猫连接失败");
            }
            timer_API.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string smsStr = http.GetHtml("http://api.aiyoubb.com/sms.ashx");
            smsStr = smsStr.Substring(1);
            smsStr = smsStr.Substring(0, smsStr.Length - 1);
            CallBak<List<BBPA.Model.SMS>> callBak = new CallBak<List<BBPA.Model.SMS>>();
            try
            {
                callBak = JsonConvert.DeserializeObject<CallBak<List<BBPA.Model.SMS>>>(smsStr);
                lb_State_API.Text = "短信接口正常";
            }
            catch (Exception err)
            {
                lb_State_API.Text = "无法连接短信接口";
                timer_API.Enabled = false;
                MessageBox.Show(smsStr+err.Message);
            }
            if (callBak.ReturnVal!=null && callBak.ReturnVal.Count > 0)
            {
                foreach (BBPA.Model.SMS sms in callBak.ReturnVal) listSMS_waitSend.Add(sms);
            }
        }
        private void timer_Send_Tick(object sender, EventArgs e)
        {
            if (listSMS_waitSend.Count == 0) return;
            BBPA.Model.SMS sms = listSMS_waitSend[0];
            bool sendOK = Sms_Send(sms._Mobile, sms.TheContent) == 1;
            EnumHelper.SendSMSstate SendSMSstate = sendOK ? EnumHelper.SendSMSstate.Success : EnumHelper.SendSMSstate.Fail;
            http.GetHtml("http://api.aiyoubb.com/sms.ashx?SMSID=" + sms.id + "&State=" + (int)SendSMSstate);

            dataGridView1.Rows.Insert(0, sms._Username, sms._Mobile, sms.Dateandtime, DateTime.Now, EnumHelper.GetEnumDescription(SendSMSstate), sms.TheContent);
            listSMS_waitSend.RemoveAt(0);
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)    //最小化到系统托盘
            {
                notifyIcon1.Visible = true;    //显示托盘图标
                this.Hide();    //隐藏窗口
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            notifyIcon1.Visible = false;
            this.Show();
            WindowState = FormWindowState.Normal;
            this.Focus();
        }

        
        
    }
}
